﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using OutdoorLib.BaseEntity.Constraction;
using OutdoorLib.BaseEntity.Contract;
using UIControls;

public partial class Ctl_ctlSideList : System.Web.UI.UserControl
{
    Func<ListViewDataItem, bool> LvUpdCmd = null;
    private Func<ContractEntity> _ContractSource = null;
    public Func<bool> SaveMethod;
    
    public Func<ContractEntity> ContractSource 
    {   get { return _ContractSource; } 
        set { 
            _ContractSource = value; 
            } 
    }

    protected void Page_Load(object sender, EventArgs e)
    {

    }
    
    public override void DataBind()
    {
        if (null == ContractSource)
            return;

        ContractSource().ContractSideItemList.Reset();
        lvSideList.DataSource = ContractSource().ContractSideItemList;
        lvSideList.DataBind();

    }

    protected ConstractionEntity GetConstractionDescribe(Side Side)
    {
        ConstractionEntity result = null;
        string ConstrID = Side.GetConstractionID(Side.ID);
        result = (ConstractionEntity)AppEnvironment.Helper.GetObject(ConstrID);
        return result;

    }
   
    protected void ItemUpdating(object sender, ListViewUpdateEventArgs e)
    {
    }
    
    private bool UpdateItem(ListViewDataItem item)
    {
        TextBox txtPrice = (TextBox)item.FindControl("txtPrice");
        TextBox_Date txtDtStart = (TextBox_Date)item.FindControl("txtDtStart");
        TextBox_Date txtDtFinish = (TextBox_Date)item.FindControl("txtDtFinish");

        HiddenField hf = (HiddenField)item.FindControl("hfCurrentID");
        TContractSide val = (ContractSource().ContractSideItemList[hf.Value] as TContractSide);
        val.DtStart = txtDtStart.TexAsDateValue;
        val.DtFinish = txtDtFinish.TexAsDateValue;
        val.Price = Convert.ToInt16(txtPrice.Text);
        if (null != SaveMethod)
            if(  SaveMethod())
                        return true;

        return false;
            
    }    

    protected void ItemCommand(object sender, ListViewCommandEventArgs e)
    {
       

        if (string.Empty == e.CommandName) return;

        ListView lv = (ListView)sender;

        switch (lv.ID)
        {
            case "lvSideList":

                LvUpdCmd = UpdateItem;
                break;

            case "lvClient":
                //LvUpdCmd = UpdateClient;
                break;
        }

        switch (e.CommandName)
        {
            case "Edit":
                {
                    ListViewDataItem d = (ListViewDataItem)e.Item;
                    lv.EditIndex = d.DataItemIndex;
                    break;
                }
            case "Insert":
                {

                    break;
                }
            case "Update":
                {
                    lv.EditIndex = -1;
                    //// обновить поле
                     if (LvUpdCmd((ListViewDataItem)e.Item))
                    {
                        lv.EditIndex = -1;                   
                    }
                    break;
                }
            case "Delete":
                {
                    //DeleteCustomer(e.CommandArgument as string);
                    break;

                }
        }

        DataBind();
        
        //SetAlertForTextBox();



    }
   
    protected void ItemEditing(object sender, ListViewEditEventArgs e)
    {



    }
    
    protected void ItemDataBound(object sender, ListViewItemEventArgs e)
    {
        //// Проверить тип строчки - если это строчка редактирования - зацепить требуемый DS

        //ListView lv = (ListView)sender;
        //switch (lv.ID)
        //{
        //    case "lvStatus":
        //        {
        //            object tv = e.Item.FindControl("tvStatus");
        //            if (null != tv)
        //            {
        //                ((TreeView)tv).DataSource = GetEmployeeSource("chTree_Status", "ctl00_cphHead_hf_tvStatus");
        //                ((TreeView)tv).DataBind();

        //            }
        //            break;
        //        }

        //    case "lvCategoria":
        //        {

        //            object tv = e.Item.FindControl("tvCategoria");
        //            if (null != tv)
        //            {
        //                ((TreeView)tv).DataSource = GetCategoriaSource("chTree_Categoria", "ctl00_cphHead_hf_tvCategoria");
        //                ((TreeView)tv).DataBind();

        //            }
        //            break;
        //        }

        //}

    }

    #region Q
    protected void bntShow_Q_Click(object sender,ImageClickEventArgs arg)
    {
        Pop_pnlQ.Show();
    }

    #endregion

    #region NewRecord
    
    #endregion

    #region Calculator
    protected void btnCalculatorClose_Click(object sender, ImageClickEventArgs arg)
    {
        popSidePriceCalculator.Hide();
    }
    protected void btnPriceCalculatorClick(object sender, ImageClickEventArgs arg)
    {
        popSidePriceCalculator.Show();
    }
    protected void btnClose_Click(object sender, ImageClickEventArgs e)
    {
        popSidePriceCalculator.Hide();
    }
    protected void btnConfirmRecomendetedPrice_Click(object sender, EventArgs e)
    {

    }
    #endregion
    protected void ImageButton1_Click(object sender, ImageClickEventArgs e)
    {
        Pop_pnlQ.Hide();

    }
   
}
